/*
 *   Copyright©2023 JoyCastle.
 *   All Rights Reserved.
 *   
 *   FileName:     UIMainShopItem.cs
 *   Author:       Administrator
 *   CreateTime:   2024/05/07 11:03:08
 *   UnityVersion: 2020.3.48f1c1
 *   Version:      1.1.9
 *   Description:
 *   
 */

using System.Collections;
using System.Collections.Generic;
using MotionFramework.Event;
using offline2048;
using UnityEngine;
using UnityEngine.UI;

public class UIMainShopItem : MonoBehaviour
{
    private CfgShop m_ShopTable;

    private UISprite m_ImgDollerIcon;
    private Text m_TxtDoller;
    private Text m_TxtPayDoller;
    private Button m_BtnPay;
    
    public void InitData(CfgShop table)
    {
        m_ShopTable = table;
        InitView();
        InitViewData();
    }

    private void InitView()
    {
        //m_ImgDollerIcon = this.transform.Find("DollarIcon").GetComponent<UISprite>();
        m_TxtDoller = this.transform.Find("Button/Text").GetComponent<Text>();
        m_TxtPayDoller = this.transform.Find("TextNumber").GetComponent<Text>();
        m_BtnPay = this.transform.Find("Button").GetComponent<Button>();
        
        m_BtnPay.onClick.AddListener(OnClickPayBtn);
    }

    private void InitViewData()
    {
        //m_ImgDollerIcon.SpriteName = m_ShopTable.IAP_Icon; 
        //m_TxtDoller.text = $"${m_ShopTable.Prices}";
        m_TxtPayDoller.text =  (m_ShopTable.GoodsNum / 100).ToString();
    }

    private void OnClickPayBtn()
    {
        DataIAP.Instance.Purchase(m_ShopTable.IAP_Key, (PurchaseResult PurchaseResult, string iapKey) =>
        {
            if (PurchaseResult == PurchaseResult.Success)
            {
                // 购买成功
                UIMainEvent.MainShopIapFinish _event = new UIMainEvent.MainShopIapFinish();
                _event.m_ShopTable = m_ShopTable;
                EventManager.Instance.SendMessage(_event);
            }
            else
            {
                UIMainEvent.MainShopIapFailed _event = new UIMainEvent.MainShopIapFailed();
                EventManager.Instance.SendMessage(_event);
            }
        });
    }
}

